let mysql = require('mysql');
let row = '';

// 开发环境
if(process.env.NODE_ENV == 'development'){
    row = {
        host:'127.0.0.1',
        user:'root',
        password:'123456',
        database:'lerov',
        port:'3306',
        connectionLimit:50,//允许连接数
        multipleStatements : true,  //是否允许执行多条sql语句
        timezone:"08:00" //大坑，必须加这一句，否则时间不对劲
    };
}
// 生产环境
else{
    row = {
        host:'tomorrow.mysql.rds.aliyuncs.com',
        user:'lerov',
        password:'Aa547546',
        database:'lerov',
        port:'3306',
        connectionLimit:50,//允许连接数
        multipleStatements : true,  //是否允许执行多条sql语句
        timezone:"08:00" //大坑，必须加这一句，否则时间不对劲
    };
}
let pool = mysql.createPool(row);

//封装数据库sql请求操作，返回的是一个包含对象的数组
let query = ( sql , ...params )=>{
     return new Promise(function(resolve,reject){
         //从连接池中拿一条链接
         pool.getConnection(function(err,connection){
            if(err){
                return reject(err);
            }
            connection.query( sql , ...params , function(error,res){
                // console.log(res);
                connection.release();
                if(error){
                    return  reject(error.sqlMessage);
                }
                resolve(res);
            });
        });
    });
 };

module.exports = {
    query
};